package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.RemoteRegionService;
import cn.wolfcode.wolf2w.business.api.domain.Destination;
import cn.wolfcode.wolf2w.business.api.domain.Region;
import cn.wolfcode.wolf2w.business.mapper.DestinationMapper;
import cn.wolfcode.wolf2w.business.mapper.RegionMapper;
import cn.wolfcode.wolf2w.business.query.DestinationQuery;
import cn.wolfcode.wolf2w.business.service.IDestinationService;
import cn.wolfcode.wolf2w.business.service.IRegionService;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 目的地Service业务层处理
 *
 * @author 王南棕
 * @date 2025-09-15
 */
@Service
@Transactional
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper,Destination> implements IDestinationService {

    @Autowired
    private RegionMapper regionMapper;
    @Override
    public IPage<Destination> queryPage(DestinationQuery qo) {
        IPage<Destination> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<Destination> queryByregionId(Long regionId) {
        List<Destination> list=new ArrayList<>();
        if(regionId==-1){
            //获取父地区id（如中国）
            List<Destination> listparent = lambdaQuery().eq(Destination::getRegionId, regionId).list();
            //判断集合是否为空
            if (listparent.isEmpty()){
                return Collections.emptyList();
            }
            //获取id集合
            List<Long> ids= listparent.stream().map(Destination::getId).collect(Collectors.toList());
            list = lambdaQuery().in(Destination::getParentId, ids).list();

        }else {
            Region region = regionMapper.selectById(regionId);
            String refIds = region.getRefIds();
            List<Long> longList = java.util.Arrays.stream(refIds.split(","))
                    .map(String::trim)                    // 去除空格
                    .map(Long::parseLong)                 // 转换为 long
                    .collect(Collectors.toList());
            list = lambdaQuery().in(Destination::getId, longList).list();
        }
        //二级
        for (Destination destination : list) {
            List<Destination> children = lambdaQuery()
                    .eq(Destination::getParentId, destination.getId())
                    .last("limit 10")
                    .list();
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public List<Destination> queryToasts(Long destId) {
        List<Destination> list=new ArrayList<>();
        createToasts(destId,list);
//        Collections.reverse(list);
        return list;
    }
    private void createToasts(Long destId,List<Destination> list){
        if (destId == null) {
            return;
        }
        Destination destination = baseMapper.selectById(destId);

        createToasts(destination.getParentId(),list);
        list.add(destination);
    }
}
